<?php

	class PostsController extends AppController {
		var $name = 'Posts';
		var $uses = array('Post');
		
		var $publicAction = array('index', 'details');
		
		var $type = 'blog';
		
		function beforeFilter() {
			parent::beforeFilter();
			$this->type = !empty($this->params['type']) ? $this->params['type'] : $this->type;
		}
		
		function beforeRender() {
			parent::beforeRender();
			$this->set('type', $this->type);
		}
		
		function index() {
			$this->paginate = array(
				'conditions' => array(
					'Post.shop_id' => Configure::read('Shop.id'),
					'Post.type' => $this->type,
					'Post.disabled' => 0,
					'Post.deleted' => 0
				),
				'fields' => array(
					'Post.id',
					'YEAR(Post.created) AS year',
					'MONTH(Post.created) AS month',
					'DAY(Post.created) AS day',
					'Post.slug',
					'Post.title',
					'Post.excerpt'
				)
			);
			
			$posts = $this->paginate('Post');
			
			$this->set(compact('posts'));
			
			$this->render($this->type);
		}
		
		function details($slug = null) {
			$details = $this->Post->find(
				'first',
				array(
					'conditions' => array(
						'Post.shop_id' => Configure::read('Shop.id'),
						'Post.slug' => $slug,
						'Post.type' => $this->type,
						'Post.disabled' => 0,
						'Post.deleted' => 0,
					)
				)
			);
			
			if (!empty($details)) {
				// 404
			}
			
			$this->set(compact('details'));
			
			$this->render($this->type . '_details');
		}
		
		function dashboard_index() {
			$posts = $this->Post->find(
				'all',
				array(
					'conditions' => array(
						'Post.shop_id' => Configure::read('Shop.id'),
						'Post.type' => $this->type,
						'Post.deleted' => 0
					),
					'fields' => array(
						'Post.id',
						'Post.title',
						'Post.content',
						'Post.publish'
					)
				)
			);
			
			$this->set(compact('posts'));
			
			if ($this->type == 'pages') {
				$this->pageTitle = __('Páginas', true);
				$this->render('dashboard_pages_index');
			} else
			if ($this->type == 'blog') {
				$this->pageTitle = __('Noticias / Blog', true);
				$this->render('dashboard_blog_index');
			}
		}
		
		function dashboard_add() {
			if (!empty($this->data)) {
				$this->data['Post']['shop_id'] = Configure::read('Shop.id');
				$this->data['Post']['type'] = $this->type;
				
				if ($this->Post->save($this->data)) {
					if ($this->type == 'pages') {
						$this->Session->setFlash(__('Se guardo la página exitosamente.', true), 'default', array('class' => 'success'));
					} else
					if ($this->type == 'blog') {
						$this->Session->setFlash(__('Se guardo la entrada exitosamente.', true), 'default', array('class' => 'success'));
					}
					$this->redirect(array('controller' => 'posts', 'action' => 'dashboard_index', 'type' => $this->type));
				}
			}
			
			if ($this->type == 'pages') {
				$this->pageTitle = __('Crear página', true);
				$this->render('dashboard_blog_form');
			} else
			if ($this->type == 'blog') {
				$this->pageTitle = __('Publicar noticia', true);
				$this->render('dashboard_blog_form');
			}
		}
		
		function dashboard_edit($id = null, $idEncrypt = null) {
			if (Security::hash($id, 'md5', true) != $idEncrypt) {
				$this->Session->setFlash(__('Es imposible editar el post, solo guíate por los links del panel de administración.', true), null, array('class' => 'warning'));
				$this->redirect(array('controller' => 'posts', 'action' => 'dashboard_index', 'type' => $this->type));
			}
			
			$this->Post->find(
				'first',
				array(
					'conditions' => array(
						'Post.id' => $id,
						'Post.shop_id' => Configure::read('Shop.id')
					)
				)
			);
			
			
			if ($this->type == 'pages') {
				$this->pageTitle = __('Modificar página', true);
				$this->render('dashboard_pages_form');
			} else
			if ($this->type == 'blog') {
				$this->pageTitle = __('Editar noticia', true);
				$this->render('dashboard_blog_form');
			}
		}
	}

?>